/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom.local;

import com.googlecode.actorom.Actor;
import com.googlecode.actorom.FailoverPolicy;
import com.googlecode.actorom.ThreadingPolicy;
import com.googlecode.actorom.impl.core.AbstractTopology;
import com.googlecode.actorom.support.FailoverPolicies;
import com.googlecode.actorom.support.ThreadingPolicies;
import net.jcip.annotations.ThreadSafe;

/**
 * {@link com.googlecode.actorom.Topology} implementation to use for local-only, in-memory,
 * actors.<br>
 * A LocalTopology instance is able to:
 * <ul>
 * <li>Locally spawn actors.</li>
 * <li>Access its own local actors by using their {@link LocalAddress}, but not actors spawned
 * by other LocalTopology instances.</li>
 * </ul>
 *
 * @author Sergio Bossa
 */
@ThreadSafe
public class LocalTopology extends AbstractTopology<LocalAddress> {

    /**
     * Create a local topology using the following defaults:
     * {@link com.googlecode.actorom.support.ThreadingPolicies#newGreenThreadingPolicy()} and
     * {@link com.googlecode.actorom.support.FailoverPolicies#newNoFailoverPolicy()}.
     */
    public LocalTopology() {
        this(ThreadingPolicies.newGreenThreadingPolicy(), FailoverPolicies.newNoFailoverPolicy());
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.ThreadingPolicy},
     * and the {@link com.googlecode.actorom.support.FailoverPolicies#newNoFailoverPolicy()} default.
     * @param threadingPolicy
     */
    public LocalTopology(ThreadingPolicy threadingPolicy) {
        this(threadingPolicy, FailoverPolicies.newNoFailoverPolicy());
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.FailoverPolicy},
     * and the {@link com.googlecode.actorom.support.ThreadingPolicies#newGreenThreadingPolicy()} default.
     * @param failoverPolicy
     */
    public LocalTopology(FailoverPolicy failoverPolicy) {
        this(ThreadingPolicies.newGreenThreadingPolicy(), failoverPolicy);
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.ThreadingPolicy} and
     * {@link com.googlecode.actorom.FailoverPolicy}.     *
     * @param threadingPolicy
     * @param failoverPolicy 
     */
    public LocalTopology(ThreadingPolicy threadingPolicy, FailoverPolicy failoverPolicy) {
        super(threadingPolicy, failoverPolicy);
    }

    public synchronized LocalAddress spawnActor(String id, Object handler) {
        LocalAddress address = new LocalAddress(id);
        boolean spawned = doSpawnActor(address, handler);
        if (spawned) {
            return address;
        } else {
            return null;
        }
    }

    public synchronized Actor getActor(LocalAddress address) {
        return doGetActor(address);
    }

    public synchronized void shutdown() {
        doShutdown();
    }
}
